﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using WhiteFlow.DevTrackingTool.UI.Models;
using WhiteFlow.Wpf.Common.Extensions;

namespace WhiteFlow.DevTrackingTool.UI.Controls
{
    /// <summary>
    /// A specialized calendar that shows specific tracking data.
    /// </summary>
    [TemplatePart(Name = "PART_TrackedTasksIcon", Type = typeof(Border))]
    public class TrackingCalendar : Calendar
    {
        #region DependencyProperties Declaration

        /// <summary>
        /// Represents the tracking data that the calendar will display.
        /// </summary>
        public static DependencyProperty TrackingSourceProperty;

        #endregion

        #region Fields

        /// <summary>
        /// The Grid element that contains the MonthView in the UI.
        /// </summary>
        private Grid PART_MonthView;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the tracking data that the calendar will display.
        /// </summary>
        public TrackingWorksheet TrackingSource
        {
            get { return GetValue(TrackingSourceProperty) as TrackingWorksheet; }
            set { SetValue(TrackingSourceProperty, value); }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="TrackingCalendar"/> class.
        /// </summary>
        public TrackingCalendar()
        {
            this.LayoutUpdated += new EventHandler(OnLayoutUpdated);
        }

        /// <summary>
        /// Static constructor.
        /// </summary>
        static TrackingCalendar()
        {
            #region DependencyProperties Registration

            TrackingSourceProperty = DependencyProperty.Register(
                "TrackingSource",
                typeof(TrackingWorksheet),
                typeof(TrackingCalendar),
                new FrameworkPropertyMetadata(null));

            #endregion
        }

        #endregion

        #region Events

        private void OnLayoutUpdated(object sender, EventArgs e)
        {
            UpdateCalendarGlyphs();
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Retrieves the Grid element that contains the MonthView in the UI.
        /// </summary>
        private void GetMonthViewGrid()
        {
            PART_MonthView = (this as DependencyObject).FindVisualChildByType<Grid>("PART_MonthView");
        }

        /// <summary>
        /// Retrieves the icon used to specify that a day contains tracked tasks.
        /// </summary>
        private Border GetTrackedTasksIcon(CalendarDayButton dayButton)
        {
            return (dayButton as DependencyObject).FindVisualChildByType<Border>("PART_TrackedTasksIcon");
        }

        private void UpdateCalendarGlyphs()
        {
            GetMonthViewGrid();

            if (PART_MonthView != null &&
                TrackingSource != null &&
                TrackingSource.Days != null &&
                TrackingSource.Days.Count > 0)
            {
                foreach (UIElement child in PART_MonthView.Children)
                {
                    if (child is CalendarDayButton)
                    {
                        CalendarDayButton dayButton = child as CalendarDayButton;
                        DateTime relatedDate = (DateTime)dayButton.DataContext;
                        TrackingDay day = TrackingSource.Days.FirstOrDefault(d => d.CalendarDay.Date.Equals(relatedDate.Date));

                        Border trackedTasksIcon = GetTrackedTasksIcon(dayButton);

                        if (trackedTasksIcon != null)
                        {
                            if (day != null)
                            {
                                trackedTasksIcon.Visibility = System.Windows.Visibility.Visible;

                                if (dayButton.IsInactive)
                                {
                                    trackedTasksIcon.Opacity = 0.3d;
                                }
                            }
                            else trackedTasksIcon.Visibility = System.Windows.Visibility.Collapsed;
                        }
                    }
                }
            }
        }

        //private TrackingDay GetTrackingDay(DateTime day)
        //{
        //    return (from d in TrackingSource.Days
        //            where DateTime.Compare(d.Date.Date, day.Date) == 0
        //            select d).SingleOrDefault();
        //}

        #endregion
    }
}